<?php 
session_start();
class yahoo extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
        private $_configs = null;
        function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','',"smarty");
            $this->load->library('OpenID/LightOpenID',base_url(),"LightOpenID");
            $this->load->model('deal/customer_model','customer_model');
        }
        public function index_(){
            $this->smarty->assign('social', "Yahoo");
            $this->smarty->display("openid/01_rd");  
        }
        public function index(){
            $msg = "Đang chuyển hướng đăng nhập đến Yahoo.";
            try {
                # Change 'localhost' to your domain name.
                $openid = $this->LightOpenID;//new LightOpenID('localhost');
                if(!$openid->mode) {       
                        $openid->identity = "https://me.yahoo.com";
                        # The following two lines request email, full name, and a nickname
                        # from the provider. Remove them if you don't need that data.
                        $openid->required = array('contact/email');
                        $openid->optional = array(
                            'namePerson', 
                            'namePerson/friendly',
                            "phone/default",
                            "postaladdress/home",
                            "postaladdressadditional/home");
                        header('Location: ' . $openid->authUrl());
                } elseif($openid->mode == 'cancel') {
                    $msg = 'Bạn đã từ chối xác nhận truy cập đến tài khoản Yahoo!';
                } else {
                    //echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
                    if($openid->validate()){
                        $_SESSION["OpenID"] = array(
                            "User"=>$openid->getAttributes(),
                            "Type"=>"Yahoo"
                        );
                        redirect(base_url("openid/yahoo/ylogin"));
                    }
                    $msg = 'Bạn chưa đăng nhập tài khoản Yahoo!';
                }
            } catch(ErrorException $e) {
                $msg = $e->getMessage();
            } 
            $this->smarty->assign('social', "Yahoo");
            $this->smarty->assign('msg', $msg);
            $this->smarty->display("openid/redirect");                     
	}
        function ylogin(){
            $vlows=array("@yahoo.com","@yahoo.com.vn","@gmail.com","@gmail.com.vn");
            if(isset($_SESSION["OpenID"]["User"]["namePerson"])){
                $Configs["name"]=$_SESSION["OpenID"]["User"]["namePerson"]; 
            }else{
                $Configs["name"]=$_SESSION["OpenID"]["User"]["contact/email"]; 
                $Configs["name"]=  str_replace($vlows, "", $Configs["name"]);
            }
            $_SESSION["D-USER"]=array(
                "CustomerID"=>"Y".strtoupper(md5($_SESSION["OpenID"]["User"]["contact/email"])),
                "CustomerName"=>$Configs["name"],
                "Mail"=>$_SESSION["OpenID"]["User"]["contact/email"],
                "Avatar"=>base_url()."deal_css/images/messenger_yahoo.png",
                "Type"=>"Yahoo"
            );
            $account=$this->customer_model->get($_SESSION["OpenID"]["User"]["contact/email"]);
            if(count($account)==0){
                $this->customer_model->insert($_SESSION["D-USER"]);
                $account=$this->customer_model->get($_SESSION["OpenID"]["User"]["contact/email"]);
                $_SESSION["D-USER"]=objectToArray($account[0]);
                $params_mail=array("Email"=> $_SESSION["OpenID"]["User"]["contact/email"]);
                $this->customer_model->insertMail($params_mail);
            }else{
                $_SESSION["D-USER"]=objectToArray($account[0]);
            }
            $this->smarty->display("openid/registerwithopenid");
        }
          
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */